package com.a2.appa3.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.network.entity.req.Reschatlist;
import com.a2.appa3.ui.activity.ChatActivity1;
import com.a2.appa3.util.OnItemClickListener;
import com.a2.appa3.util.SYSOUtils;
import com.a2.appa3.util.WebSocketUtils2;
import com.a2.appa3.util.pinyinutil;

import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.List;

/**
 * Created by gjz on 9/4/16.
 */
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder> implements OnItemClickListener {

    private List<Reschatlist.FriendsList> contacts;
    private List<String> contactspinyin;
    private int layoutId;
    private Context context;

    public ContactsAdapter(Context context, List<Reschatlist.FriendsList> contacts, List<String> contactspinyin, int layoutId) {
        this.contacts = contacts;
        this.contactspinyin = contactspinyin;
        this.layoutId = layoutId;
        this.context = context;
    }

    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(layoutId, null);
        return new ContactsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ContactsViewHolder holder, int position) {
        Reschatlist.FriendsList contact = contacts.get(position);
        if (position == 0) {
            holder.tvIndex.setVisibility(View.GONE);
            holder.tvIndex.setText(contactspinyin.get(position));
        } else {
            //判断前后是否一样
            String str = contacts.get(position).nickname;
            String str1 = (String) str.subSequence(0, 1);
            String str2 = pinyinutil.getAlpha(str1);


            String astr = contacts.get(position - 1).nickname;
            String astr1 = (String) astr.subSequence(0, 1);
            String astr2 = pinyinutil.getAlpha(astr1);


            if (!astr2.equals(str2)) {
                holder.tvIndex.setVisibility(View.VISIBLE);
                boolean flag = ishanzi(contact.nickname);
                if (flag) {
                    holder.tvIndex.setText(str2);
                } else {
                    String str4 = (String) contact.nickname.subSequence(0, 1);
                    holder.tvIndex.setText(str4);
                }
            } else {
                holder.tvIndex.setVisibility(View.GONE);
            }
        }
        holder.tvName.setText(contact.remark);
        Glide.with(context).load(HttpAdress.getuseAdress() +
                contact.img).error(R.mipmap.default_head).into(holder.ivAvatar);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Info.mdatas.clear();
                    String str = "";
                    final JSONObject result = new JSONObject();
                    result.put("controller", "Action");
                    result.put("action", "openWindow");
                    result.put("type", "users");
                    result.put("id", contact.user_id + "");
                    SYSOUtils.sOut_("ContactsAdapter生成json为" + result.toString());
                    String text1 = URLEncoder.encode(result.toString(), "utf-8");
                    String encodeWord = null;
                    encodeWord = Base64.encodeToString(text1.getBytes("utf-8"), Base64.DEFAULT);
                    str = "type=ins&message=" + encodeWord;
                    SYSOUtils.sOut_("ContactsAdapter最终结果为" + str);
                    WebSocketUtils2.getInstance().sendMsg(str);
                    Intent intent = new Intent(context, ChatActivity1.class);
                    intent.putExtra("chatname", contact.remark);
                    context.startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return contacts.size();
    }

    @Override
    public void onItemClick(View view, int position) {

    }

    class ContactsViewHolder extends RecyclerView.ViewHolder {
        public TextView tvIndex;
        public ImageView ivAvatar;
        public TextView tvName;

        public ContactsViewHolder(View itemView) {
            super(itemView);
            tvIndex = (TextView) itemView.findViewById(R.id.tv_index);
            ivAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }

    Boolean ishanzi(String characters) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < characters.length(); i++) {

            char ch = characters.charAt(i);
            if ((ch >> 7) == 0) {
                // 不是汉字
                return false;
            } else {
                return true;

            }
        }
        return false;
    }
}